package com.zhuchaooasystem.ui.home.service;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.zhuchaooasystem.R;
import com.zhuchaooasystem.adapter.holder.ServiceScheduleHolder;
import com.zhuchaooasystem.base.BaseActivity;
import com.zhuchaooasystem.common.Key;
import com.zhuchaooasystem.entity.ServiceDetailEntity;
import com.zhuchaooasystem.entity.ServiceProgressEntity;
import com.zhuchaooasystem.presenter.home.serviceschedule.ServiceScheduleContract;
import com.zhuchaooasystem.presenter.home.serviceschedule.ServiceSchedulePresenter;
import com.zhuchaooasystem.utils.AppUtils;
import com.zhuchaooasystem.utils.ToastUtil;
import com.zhuchaooasystem.widget.StateButton;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ServiceScheduleActivity extends BaseActivity<ServiceSchedulePresenter> implements
        ServiceScheduleContract.View {


    @BindView(R.id.actionbar_iv_back)
    ImageView actionbarIvBack;
    @BindView(R.id.actionbar_txt_title)
    TextView actionbarTxtTitle;
    @BindView(R.id.act_service_schedule_code)
    TextView actServiceScheduleCode;
    @BindView(R.id.act_service_schedule_pay_state)
    TextView actServiceSchedulePayState;
    @BindView(R.id.act_service_schedule_create_time)
    TextView actServiceScheduleCreateTime;
    @BindView(R.id.act_service_schedule_contact_name)
    TextView actServiceScheduleContactName;
    @BindView(R.id.act_service_schedule_address)
    TextView actServiceScheduleAddress;
    @BindView(R.id.act_service_schedule_livation)
    ImageView actServiceScheduleLivation;
    @BindView(R.id.act_service_schedule_remark)
    TextView actServiceScheduleRemark;
    @BindView(R.id.act_setvice_schedule_delay_tv)
    TextView actSetviceScheduleDelayTv;
    @BindView(R.id.act_setvice_schedule_start_tv)
    TextView actSetviceScheduleStartTv;
    @BindView(R.id.service_schedule_content_layout)
    LinearLayout serviceScheduleContentLayout;
    @BindView(R.id.act_setvice_schedule_service_num)
    TextView actSetviceScheduleServiceNum;
    @BindView(R.id.act_setvice_schedule_service_day_num)
    TextView actSetviceScheduleServiceDayNum;
    @BindView(R.id.act_setvice_schedule_scroll)
    NestedScrollView scrollView;
    @BindView(R.id.act_service_schedule_recyclerView)
    EasyRecyclerView recyclerView;
    @BindView(R.id.act_service_schedule_good_num)
    TextView tv_GoodNum;
    @BindView(R.id.act_service_schedule_more_good)
    StateButton btn_MoreGood;

    private ServiceDetailEntity.ResultBean.SuccessfulBean bean;

    private RecyclerArrayAdapter adapter;

    private List<ServiceProgressEntity.ResultBean.SuccessfulBean.DetailsBean> list_good;

    private ServiceProgressEntity bean_service_progress;

    @Override
    protected int getLayout() {
        return R.layout.activity_service_schedule;
    }

    @Override
    protected ServiceSchedulePresenter getPresenter() {
        return new ServiceSchedulePresenter(mContext, this);
    }

    @Override
    protected void initVariables() {
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        bean = (ServiceDetailEntity.ResultBean.SuccessfulBean)
                bundle.getSerializable(Key.SERVICE_DETAIL_ENTITY);
    }

    @Override
    protected void initView() {

        list_good = new ArrayList<>();

        actionbarTxtTitle.setText("服务进度详情");
        actionbarIvBack.setVisibility(View.VISIBLE);
        actServiceScheduleCode.setText("订单号：" + bean.getAnonymousModel().getOrderCode());
        actServiceScheduleCreateTime.setText("创建日期：" + bean.getAnonymousModel().getCreatetime());
        actServiceScheduleContactName.setText(bean.getAnonymousModel().getContactName());
        actServiceScheduleAddress.setText(bean.getAnonymousModel().getAddress());
        actServiceScheduleRemark.setText("订单备注：" + bean.getServiceModel().getServiceRemark());
        actServiceSchedulePayState.setText(bean.getAnonymousModel().getOrderStatus());
        actSetviceScheduleServiceNum.setText(bean.getServiceModel().getSchedule() + "");
        actSetviceScheduleServiceDayNum.setText(bean.getServiceModel().getTimeLength() + "");

        //总体进度
        actSetviceScheduleDelayTv.setText("总体进展：" + bean.getServiceModel().getServicrItem().getSop_WorkDays());
        actSetviceScheduleStartTv.setText(bean.getServiceModel().getServicrItem().getSop_WotkMsg());

        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));

        recyclerView.setAdapter(adapter = new RecyclerArrayAdapter(mContext) {
                    @Override
                    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {

                        return new ServiceScheduleHolder(parent);
                    }
                }
        );


        mPresenter.downServiceListData(bean.getAnonymousModel().getOrderid()+"");
//        mPresenter.downServiceListData("66021");


    }

    @Override
    protected void initDataAndLoadData() {

    }


    @OnClick({R.id.actionbar_iv_back, R.id.act_service_schedule_livation, R.id.act_service_schedule_more_good})
    public void onClick(View view) {
        switch (view.getId()) {
            //返回
            case R.id.actionbar_iv_back:
                ServiceScheduleActivity.this.finish();
                break;
            //定位
            case R.id.act_service_schedule_livation:
                Bundle data = new Bundle();
                data.putString("address", bean.getAnonymousModel().getAddress());
                AppUtils.jump(mContext, ServiceAddMapActivity.class, data);
                break;
            //展示更多商品
            case R.id.act_service_schedule_more_good:

                if (btn_MoreGood.getText().toString().equals("查看更多商品")) {
                    btn_MoreGood.setText("收起更多商品");
                    loadMoreGood();
                } else if (btn_MoreGood.getText().toString().equals("收起更多商品")) {
                    btn_MoreGood.setText("查看更多商品");
                    showTwoGood();
                }

                break;

        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    /**
     * 服务明细
     *
     * @param entity
     */
    @Override
    public void showServiceListData(ServiceProgressEntity entity) {
        scrollView.setVisibility(View.VISIBLE);
        if (null != entity.getResult().getError()) {
            ToastUtil.showShort(mContext, entity.getResult().getError());
        } else {
            for (int i = 0; i < entity.getResult().getSuccessful().getList().size(); i++) {
                View view = LayoutInflater.from(this).inflate(R.layout.item_recycleview_service_schedule, null);

                TextView tv_line = (TextView) view.findViewById(R.id.item_recycleview_service_schedule_line);
                TextView tv_code = (TextView) view.findViewById(R.id.item_recycleview_service_schedule_code);
                TextView tv_state = (TextView) view.findViewById(R.id.item_recycleview_service_schedule_state);
                TextView tv_service_name = (TextView) view.findViewById(R.id.item_recycleview_service_schedule_service_name);
                TextView tv_service_people = (TextView) view.findViewById(R.id.item_recycleview_service_schedule_service_people);
                TextView tv_time = (TextView) view.findViewById(R.id.item_recycleview_service_schedule_time);

                TextView tv_early_start_time = (TextView) view.findViewById(R.id.act_service_schedule_early_start_time);
                TextView tv_last_start_time = (TextView) view.findViewById(R.id.act_service_schedule_last_start_time);
                TextView tv_start_time = (TextView) view.findViewById(R.id.act_service_schedule_start_time);
                TextView tv_finish_time = (TextView) view.findViewById(R.id.act_service_schedule_finish_time);
                TextView tv_project_time = (TextView) view.findViewById(R.id.act_service_schedule_project_time);
                TextView tv_real_time = (TextView) view.findViewById(R.id.act_service_schedule_real_time);
                TextView tv_delay_time = (TextView) view.findViewById(R.id.act_service_schedule_delay_time);

                tv_code.setText(entity.getResult().getSuccessful().getList().get(i).getServiceId() + "");
                tv_state.setText(entity.getResult().getSuccessful().getList().get(i).getStatus());
                tv_service_name.setText(entity.getResult().getSuccessful().getList().get(i).getServiceName());
                tv_service_people.setText("服务人：" + entity.getResult().getSuccessful().getList().get(i).getServiceUserName());
                tv_time.setText("预约时间：" + entity.getResult().getSuccessful().getList().get(i).getReservation());

                tv_early_start_time.setText("最早开始时间：" + entity.getResult().getSuccessful().getList().get(i).getTheFirst());
                tv_last_start_time.setText("最晚开始时间：" + entity.getResult().getSuccessful().getList().get(i).getThelatest());

                if (entity.getResult().getSuccessful().getList().get(i).getStatus().equals("开始")) {
                    tv_start_time.setText("开始时间：" + entity.getResult().getSuccessful().getList().get(i).getTartTodoTime());
                    tv_start_time.setVisibility(View.VISIBLE);
                } else if (entity.getResult().getSuccessful().getList().get(i).getStatus().equals("完成") ||
                        entity.getResult().getSuccessful().getList().get(i).getStatus().equals("已取消")) {
                    tv_start_time.setText("开始时间：" + entity.getResult().getSuccessful().getList().get(i).getTartTodoTime());
                    tv_start_time.setVisibility(View.VISIBLE);
                    tv_finish_time.setText("完成时间：" + entity.getResult().getSuccessful().getList().get(i).getEdnTodoTime());
                    tv_finish_time.setVisibility(View.VISIBLE);
                }

                tv_project_time.setText("工期" + entity.getResult().getSuccessful().getList().get(i).getSop_timeDays() + "天");
                tv_real_time.setText(entity.getResult().getSuccessful().getList().get(i).getSop_WorkDays());
                tv_delay_time.setText(entity.getResult().getSuccessful().getList().get(i).getSop_WotkMsg());

                //给左侧的线设置颜色
                if (entity.getResult().getSuccessful().getList().get(i).getServiceId() == bean.getAnonymousModel().getID()) {
                    tv_line.setBackgroundResource(R.color.color_FF9C01);
                } else {
                    tv_line.setBackgroundResource(R.color.color_1bc3ad);

                }
                serviceScheduleContentLayout.addView(view);
            }
        }
    }

    /**
     * 商品明细
     *
     * @param entity
     */
    @Override
    public void showGoodListData(ServiceProgressEntity entity) {

        bean_service_progress = entity;

        //判断显示更多商品是不是显示
        if (entity.getResult().getSuccessful().getDetails().size() > 2) {
            btn_MoreGood.setVisibility(View.VISIBLE);
        } else {
            btn_MoreGood.setVisibility(View.GONE);
        }

        //商品数量
        tv_GoodNum.setText(entity.getResult().getSuccessful().getDetails().size() + "");

        //给商品详情添加数据---默认展示两条数据
        if (entity.getResult().getSuccessful().getDetails().size() < 3) {
            adapter.clear();
            adapter.addAll(entity.getResult().getSuccessful().getDetails());
        } else {
            for (int i = 0; i < 2; i++) {
                list_good.add(entity.getResult().getSuccessful().getDetails().get(i));
            }
            adapter.clear();
            adapter.addAll(list_good);
        }
    }

    private void loadMoreGood() {
        adapter.clear();
        adapter.addAll(bean_service_progress.getResult().getSuccessful().getDetails());
    }

    private void showTwoGood() {
        adapter.clear();
        list_good = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            list_good.add(bean_service_progress.getResult().getSuccessful().getDetails().get(i));
        }
        adapter.addAll(list_good);
    }
}
